using System;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class SelectImageHelper : NodeHelper
    {
        public Color disableColor = new(255, 255, 255, 0.4f);
        public Color originColor = new(255, 255, 255);
        public List<SelectImageItem> list;
        public Sprite defaultSprite;
        public Texture defaultTexture;

        [SelectRef(UIKey.DATA_KEY)] public SelectKey disabledKey;
        public bool invert;
        public bool raw;
        protected bool disabled;
        private Image _Image;
        private RawImage _RawImage;

        private Image Image
        {
            get
            {
                if (_Image == null) _Image = GetComponent<Image>();
                return _Image;
            }
        }

        private RawImage RawImage
        {
            get
            {
                if (_RawImage == null) _RawImage = GetComponent<RawImage>();
                return _RawImage;
            }
        }

        protected override BindDataType bindDataType => BindDataType.String;
        
        public override void SetData(JSONObject json)
        {
            if (!disabledKey.IsEmpty())
            {
                disabled = invert ? !disabledKey.Get(json).AsBool : disabledKey.Get(json).AsBool;
                if (disabled)
                    Disabled();
                else
                    Enabled();
            }

            base.SetData(json);
        }

        protected override void BindData(string s)
        {
            if (StringTools.IsEmpty(s)) return;
            if (raw)
            {
                Texture texture = null;
                for (var i = 0; i < list.Count; i++)
                    if (s == list[i].key)
                    {
                        texture = list[i].texture;
                        break;
                    }

                if (texture != null)
                {
                    RawImage.texture = texture;
                }
                else if (defaultTexture != null)
                {
                    RawImage.texture = defaultTexture;
                }
            }
            else
            {
                Sprite sprite = null;
                for (var i = 0; i < list.Count; i++)
                    if (s == list[i].key)
                    {
                        sprite = list[i].image;
                        break;
                    }

                if (sprite != null)
                {
                    Image.sprite = sprite;
                }
                else if (defaultSprite != null)
                {
                    Image.sprite = defaultSprite;
                }
            }
        }

        protected override void BindData(bool b)
        {
            if (b)
                Enabled();
            else
                Disabled();
        }

        public void Disabled()
        {
            if (raw)
                RawImage.color = disableColor;
            else
                Image.color = disableColor;
        }

        public void Enabled()
        {
            if (raw)
                RawImage.color = originColor;
            else
                Image.color = originColor;
        }

        [Serializable]
        public class SelectImageItem
        {
            public string key;
            public Sprite image;
            public Texture texture;
        }
    }
}